package com.crazybytex.fragment.fastjson;



import com.alibaba.fastjson2.JSON;
import com.crazybytex.fragment.fastjson.beans.RequestBean;
import com.crazybytex.fragment.fastjson.beans.RequestBody;
import com.crazybytex.fragment.fastjson.beans.ResponseBean;
import com.crazybytex.fragment.fastjson.sample.TestResponse;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;

/**
 * 通用的执行器 样例,比如可以设计为模板模式
 * 抽象一些通用处理逻辑,子类负责具体的某些请求响应的实现  并且通过子类指定请求与响应的泛型
 * 比如通用的封装的发起http请求 通用的rpc调用处理等
 *
 * @param <RQ>
 * @param <RS>
 * @author 程序员潇然 https:crazybytex.com
 */
public class HttpExecutor<RQ, RS> {

    public ResponseBean<RS> doPost(RequestBean<RQ> requestBean) {

        //此处假设为执行某些逻辑处理、调用之后,获取到的响应结果,结果是json的
        String resultJson1 = "{\n" +
                "    \"code\": 45,\n" +
                "    \"msg\": \"laboris eu irure voluptate\"\n" +
                "}";

        String resultJson2 = "{\"code\":1,\"data\":{\"a\":1,\"b\":5,\"x\":\"feffewef\"},\"msg\":\"成功\"}";


        String resultJson3 = "{\n" +
                "  \"code\": 1,\n" +
                "  \"msg\": \"识别成功\",\n" +
                "  \"data\": [\n" +
                "    {\n" +
                "      \"classId\": 1,\n" +
                "      \"x1\": 0,\n" +
                "      \"y1\": 0,\n" +
                "      \"x2\": 200,\n" +
                "      \"y2\": 200\n" +
                "    },\n" +
                "    {\n" +
                "      \"classId\": 2,\n" +
                "      \"x1\": 5,\n" +
                "      \"y1\": 10,\n" +
                "      \"x2\": 36,\n" +
                "      \"y2\": 21\n" +
                "    },\n" +
                "    {\n" +
                "      \"classId\": 3,\n" +
                "      \"x1\": 44,\n" +
                "      \"y1\": 55,\n" +
                "      \"x2\": 66,\n" +
                "      \"y2\": 77\n" +
                "    },\n" +
                "    {\n" +
                "      \"classId\": 4,\n" +
                "      \"x1\": 24,\n" +
                "      \"y1\": 22,\n" +
                "      \"x2\": 58,\n" +
                "      \"y2\": 96\n" +
                "    }\n" +
                "  ]\n" +
                "}";

        String resultJson = resultJson2;
/******************************************************************************************************************/
        ResponseBean<RS> responseBean = (ResponseBean<RS>) JSON.parseObject(resultJson, ResponseBean.class);
        Map<String, Object> map = JSON.parseObject(resultJson, Map.class);
        if (map.get("data") != null) {
            //请求参数
            Type requestParameterizedType = ((ParameterizedType) getClass().getGenericSuperclass())
                    .getActualTypeArguments()[0];

            //响应参数
            Type responseParameterizedType = ((ParameterizedType) getClass().getGenericSuperclass())
                    .getActualTypeArguments()[1];
            responseBean.setData(JSON.parseObject(JSON.toJSONString(map.get("data")), responseParameterizedType));

        }

        return responseBean;
    }


    public static void main(String[] args) {
        /*
        注意,下面是简化形式的子类创建,匿名类,注意new 后面有一个{}
        这样就可以指定 请求以及响应类型
         */
//        RequestBean<RequestBody> requestBean1 = new RequestBean<>();
//        ResponseBean<ResponseBody> responseBean1 = new HttpExecutor<RequestBody, ResponseBody>() {
//        }.doPost(requestBean1);
//        System.out.println(responseBean1);


        RequestBean<RequestBody> requestBean2 = new RequestBean<>();
        ResponseBean<TestResponse> responseBean2 = new HttpExecutor<RequestBody, TestResponse>() {
        }.doPost(requestBean2);
        System.out.println(responseBean2);
//
//        RequestBean<RequestBody> requestBean3 = new RequestBean<>();
//        ResponseBean<List<TestListType>> responseBean3 = new HttpExecutor<RequestBody, List<TestListType>>() {
//        }.doPost(requestBean3);
//        System.out.println(responseBean3);


    }
}
